/*!
 * @file SceneHandler.cpp
 * @author Rocco Martino
 */
/***************************************************************************
 *   Copyright (C) 2015 by Rocco Martino                                   *
 *   martinorocco@gmail.com                                                *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Lesser General Public License as        *
 *   published by the Free Software Foundation; either version 2.1 of the  *
 *   License, or (at your option) any later version.                       *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU Lesser General Public      *
 *   License along with this program; if not, write to the                 *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

/* ======================================================================= */
/* ....................................................................... */
#include <ooViewer/Root>
#include <ooViewer/SceneHandler>

#include <osgODE/Notify>
#include <osgODE/RigidBody>
#include <osgODE/World>
/* ....................................................................... */
/* ======================================================================= */




using namespace ooViewer ;




/* ======================================================================= */
/* ....................................................................... */
SceneHandler::SceneHandler( Root* root ):
    m_root          ( root )
{
}
/* ....................................................................... */
/* ======================================================================= */




/* ======================================================================= */
/* ....................................................................... */
SceneHandler::SceneHandler(const SceneHandler& other):
    ooGame::SceneActuator::Handler  ( other ),
    m_root      ( other.m_root )
{
}
/* ....................................................................... */
/* ======================================================================= */




/* ======================================================================= */
/* ....................................................................... */
SceneHandler::~SceneHandler(void)
{
}
/* ....................................................................... */
/* ======================================================================= */




/* ======================================================================= */
/* ....................................................................... */
void
SceneHandler::operator()( const ooGame::SceneActuator* sa, osgODE::ODEObject* object, ooGame::Game* game )
{

    switch( sa->getMode() )
    {
        case ooGame::SceneActuator::RESUME:
            PS_FATAL("ooViewer::SceneHandler::operator(%p): RESUME is not supported", this) ;
        break ;




        case ooGame::SceneActuator::SUSPEND:
            PS_FATAL("ooViewer::SceneHandler::operator(%p): SUSPEND is not supported", this) ;
        break ;




        case ooGame::SceneActuator::REMOVE:
            PS_FATAL("ooViewer::SceneHandler::operator(%p): REMOVE is not supported", this) ;
        break ;




        case ooGame::SceneActuator::ADDBACK:
            PS_FATAL("ooViewer::SceneHandler::operator(%p): ADDBACK is not supported", this) ;
        break ;




        case ooGame::SceneActuator::ADDFRONT:
            PS_FATAL("ooViewer::SceneHandler::operator(%p): ADDFRONT is not supported", this) ;
        break ;




        case ooGame::SceneActuator::CAMERA:
        {
            osgODE::RigidBody*  body = object->asRigidBody() ;

            PS_ASSERT1( body ) ;
            PS_ASSERT1( body->getWorld() ) ;

            osgODE::Events* events = body->getWorld()->getFrontEventsBuffer() ;

            const osgODE::Events::EventList&    event_list = events->getEventList() ;

            for( unsigned int i=0; i<event_list.size(); i++ ) {

                if( event_list[i].first->getEventType() == osgGA::GUIEventAdapter::FRAME ) {
                    events->requestViewMatrix( event_list[i].second.get(), osg::Matrix::inverse(body->getMatrix()) ) ;
                }
            }
        }
        break ;




        case ooGame::SceneActuator::SET:
            m_root->requestScene( sa->getSceneName() ) ;
        break ;




        case ooGame::SceneActuator::RESTART:
            PS_FATAL("ooViewer::SceneHandler::operator(%p): RESTART is not supported", this) ;
        break ;
    }
}
/* ....................................................................... */
/* ======================================================================= */
